﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using System.IO;
using System.Data.SqlClient;
using System.Drawing.Imaging;

namespace QuanLyHangXeKhach
{
    public partial class frmCapNhatNhanVien : UserControl
    {
        CapNhatNhanVien cnnv = new CapNhatNhanVien();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmCapNhatNhanVien));
        DieuKhien dk = new DieuKhien();
        string imagename;//lưu tên của hình

        public frmCapNhatNhanVien()
        {
            InitializeComponent();
        }

        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcnnv;
                buttonX1.Command = newValue.showcaptk;
            }
            else
            {
                btndong.Command = null;
                buttonX1.Command = null;
            }
        }

        #endregion

        private void frmCapNhatNhanVien_Load(object sender, EventArgs e)
        {
            txttimkiem.WatermarkText = "Tìm kiếm";

            cobgioitinh.Items.Add("Nam");
            cobgioitinh.Items.Add("Nữ");
            cobgioitinh.Items.Add("Khác");

            Loadfrm();
        }

        private void Loadfrm()
        {
            label6.Text = "Có " + cnnv.nvchuacotk() + " nhân viên chưa có tài khoản";
            List<nhanvien> nvpt = cnnv.laynhanvien();
            advTree1.Nodes.Clear();
            foreach (var row in nvpt)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.ma, row.ten, row.gioitinh, row.ngaysinh,row.noisinh, row.diachi, row.cmnd, row.sodt));
            }
        }

        private Node CreatenoidenRow(string ma, string ten, string giotinh, string ngay,string noisinh, string diachi, string cmnd, string sdt)
        {
            Node node = new Node(ma);
            node.Cells.Add(new Cell(ten));
            node.Cells.Add(new Cell(giotinh));
            node.Cells.Add(new Cell(ngay));
            node.Cells.Add(new Cell(noisinh));
            node.Cells.Add(new Cell(diachi));
            node.Cells.Add(new Cell(cmnd));
            node.Cells.Add(new Cell(sdt));
           
            return node;
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                txtmanv.Text = node.Cells[0].Text;
                txthoten.Text = node.Cells[1].Text;
                cobgioitinh.Text = node.Cells[2].Text;
                maskngaysinh.Text = node.Cells[3].Text.Replace("/", "");
                txtnoisinh.Text = node.Cells[4].Text;
                txtdiachi.Text = node.Cells[5].Text;                
                txtCMND.Text = node.Cells[6].Text;
                txtsdt.Text = node.Cells[7].Text;

                Image img = Image.FromStream(cnnv.layhinhnv(txtmanv.Text));
                if (img == null) return;
                picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                picanhdaidien.Image = img;
            }
            catch (Exception) { }
        }

        public void xoatrang()
        {
            picanhdaidien.Image = ((System.Drawing.Image)(resources.GetObject("picanhdaidien.Image")));
            txtCMND.Text = "";
            txtdiachi.Text = "";
            txthoten.Text = "";
            txtsdt.Text = "";
            txttimkiem.Text = "";            
            cobgioitinh.Text = "";
            txtnoisinh.Text = "";
            maskngaysinh.Text = "";
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Text = "Lưu";
            btnxoa.Text = "Hủy bỏ";
            btnthem.Enabled = false;
            xoatrang();
            txtmanv.Text = dk.NextID(cnnv.manvlonnhat().ToString(), "NV");
            this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txthoten.Text == "" || txtdiachi.Text == "" || txtnoisinh.Text == "" || maskngaysinh.Text == "")
                MessageBoxEx.Show("Nhập đủ thông tin trước khi tiếp tục", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (btnsua.Text == "Lưu")
                {
                    MemoryStream stream = new MemoryStream();
                    picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                    cnnv.themnv(txtmanv.Text, txthoten.Text, cobgioitinh.Text, dk.catngay(maskngaysinh.Text), txtnoisinh.Text, txtdiachi.Text, txtsdt.Text, txtCMND.Text, stream);
                    MessageBoxEx.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    xoatrang();
                    txtmanv.Text = dk.NextID(cnnv.manvlonnhat().ToString(), "NV");
                    Loadfrm();

                }
                else
                {
                    MemoryStream stream = new MemoryStream();
                    picanhdaidien.Image.Save(stream, ImageFormat.Jpeg);
                    cnnv.suanv(txtmanv.Text, txthoten.Text, cobgioitinh.Text, dk.catngay(maskngaysinh.Text), txtnoisinh.Text, txtdiachi.Text, txtsdt.Text, txtCMND.Text, stream);
                    MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadfrm();
                }
            }
        }

        private void btnduyetdanh_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog dldlg = new OpenFileDialog();
                dldlg.InitialDirectory = @":d\";
                dldlg.Filter = "Image File(*.jpg;*.bmp;*.gif)|*jpg;*bmp;*gif";
                if (dldlg.ShowDialog() == DialogResult.OK)
                {
                    imagename = dldlg.FileName;
                    Bitmap newimg = new Bitmap(imagename);
                    picanhdaidien.SizeMode = PictureBoxSizeMode.StretchImage;
                    picanhdaidien.Image = (Image)newimg;
                }
                dldlg = null;

            }
            catch (System.ArgumentException ae)
            {
                imagename = " ";
                MessageBox.Show(ae.Message.ToString());

            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnxoa.Text == "Hủy bỏ")
            {
                btnthem.Enabled = true;
                btnsua.Text = "Sửa";
                btnxoa.Text = "Xóa";
                xoatrang();
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txtmanv.Text == "")
                    MessageBoxEx.Show("Chọn đối tượng cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    DialogResult dr = MessageBoxEx.Show("Bạn muốn xóa nhân viên " + txthoten.Text + " này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (dr == DialogResult.Yes)
                    {
                        cnnv.xoanv(txtmanv.Text);
                        MessageBoxEx.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xoatrang();
                        txtmanv.Text = "";
                        Loadfrm();
                    }
                }
            }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            List<nhanvien> nvpt = cnnv.laynhanvien(txttimkiem.Text);
            advTree1.Nodes.Clear();
            foreach (var row in nvpt)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.ma, row.ten, row.gioitinh, row.ngaysinh, row.noisinh, row.diachi, row.cmnd, row.sodt));
            }
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void maskngaysinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

       
    }
}
